package day240528.exceptions;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

public class ExceptionDemo {
    public static void main(String[] args) {
        //java.lang.StackOverflowError
        //stackOverflowError();




        //java.lang,OutOfMemoryError
//        outOfMemoryError();



        System.out.println("===== ===== =====");



        //java.lang.ArrayIndexOutOfBoundsException
//        arrayIndexOutOfBoundsException();

        // java.lang.NumberFormatException
        //numberFormatException();


        // java.lang.ArithmeticException: / by zero
        //arithmeticException();


        // java.lang.NullPointerException
        //nullPointerException();


        System.out.println("=== === ===");


        // java.io.FileNotFoundException
        try {//自己能解决的话
            fileNotFoundException();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);//解决逻辑：1.写日志2.发邮件3...

        }


        // java.io.IOException
        //ioException();
    }

    private static void nullPointerException() {
        int[][] xx = new int[2][];
        xx[0][0] = 1;

//        String s = null;
//        System.out.println(s.length());
    }

    private static void stackOverflowError() {
        // Git -> Log -> Filter by Commit Message or Hashes
    }

    private static void outOfMemoryError() {
        String[] integers = new String[1024 * 1024 * 1024];
    }

    private static void arrayIndexOutOfBoundsException() {
        int[] is = new int[] {1, 2, 3};
        System.out.println(is[3]);
    }

    private static void numberFormatException() {
        Scanner scanner = new Scanner(System.in);
        Integer.parseInt(scanner.next());
    }

    private static void arithmeticException() {
        int i = 3 / 0;
    }

    private static void fileNotFoundException()throws FileNotFoundException {
        //自己没能力处理异常就往上throws
        FileInputStream FileInputStream = new FileInputStream("config.properties");
        //checked exception 编译时异常 vs 运行时异常 unchecked exception (RuntimeException)
        //structure
    }

    private static void ioException() {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("config.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(e.getMessage());

        }

}
}
